package com.q.springboot.project.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
@Slf4j
public class SpringRetryAnnotationService {

    /**
     * 如果失败，定义重试3次，重试间隔为3s，指定恢复名称,指定监听器
     */

    @Retryable(value = RuntimeException.class, maxAttempts = 3, backoff = @Backoff(value = 3000L), listeners = {"myRetryListener"})
    public void test(String a) {
        log.info("test" + LocalTime.now());
        hello();
    }

    @Recover
    public void testRecover(RuntimeException runtimeException, String a) {
        log.info("记录" + LocalDateTime.now() + runtimeException.getLocalizedMessage() + "\n" + a);
    }

    public void hello() {
        throw new RuntimeException("11111`");
    }
}
